package com.wenx.guava;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

public class GuavaCache2 {

    static Cache<String,String> userNameCache =
            CacheBuilder.newBuilder()
                    .maximumSize(100L)  //缓存大小
                    .expireAfterAccess(5, TimeUnit.SECONDS)  //5s没有访问则失效
                    .build();

    public static void main(String[] args) throws Exception {

        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s = sc.nextLine();
            String s1 = userNameCache.get(s, new Callable<String>() {
                @Override
                public String call() {
                    Random r = new Random();
                    int i = r.nextInt(100);
                    return "你好，我是"+s+"，编号："+i;
                }
            });
            System.out.println("s1 = " + s1);
            //显示缓存aa值
            //userNameCache.put("aa","bb");
        }

    }

}
